package com.stox.shared.core.model;

import com.stox.shared.core.model.enumeration.ScripType;
import com.stox.shared.core.model.intf.HasScrip;
import com.stox.shared.core.util.StringUtil;

public class Scrip extends AbstractModel implements HasScrip {

	private Long exchangeId;
	private final ScripType scripType;

	public Scrip(String text) {
		String[] tokens = text.split(",");
		if(0 < tokens.length && StringUtil.hasText(tokens[0])) exchangeId = Long.parseLong(tokens[0]);
		if(1 < tokens.length && StringUtil.hasText(tokens[1])) scripType = ScripType.getByOrdinal(Integer.parseInt(tokens[1])); else throw new IllegalArgumentException();
		if(2 < tokens.length && StringUtil.hasText(tokens[2])) setId(Long.parseLong(tokens[2]));
		if(3 < tokens.length && StringUtil.hasText(tokens[3])) setName(tokens[3]);
	}
	
	@Override
	public Long getScripId() {
		return getId();
	}
	
	@Override
	public Scrip getScrip() {
		return this;
	}
	
	@Override
	public void setScrip(Scrip scrip) {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public String toString() {
		return String.valueOf(exchangeId)+","+getScripType().ordinal()+","+getId()+","+getName();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(null == obj || !(obj instanceof Scrip)){
			return false;
		}
		Scrip other = (Scrip)obj;
		
		if(null == getId() || null == other.getId()){
			return false;
		}
		return getId().equals(other.getId());
	}
	
	public Scrip(ScripType scripType) {
		this.scripType = scripType;
	}
	
	public ScripType getScripType() {
		return scripType;
	}
	
	public Long getExchangeId() {
		return exchangeId;
	}
	
	public void setExchangeId(Long exchangeId) {
		this.exchangeId = exchangeId;
	}
	
}
